package com.zf.zfed.audit.ui

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.zf.zfed.audit.R
import com.zf.zfed.audit.tools.extension.dpFromDimen
import com.zf.zfed.audit.tools.extension.spFromDimen

/**
 * 音量设置项控件
 * 包含标题和音量滑动条
 */
@SuppressLint("ResourceAsColor", "RtlHardcoded")
class VolumeSetItem @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {

    // 名称
    private var itemName: TextView
    
    // 音量滑动条
    private var volumeSlider: VolumeSlider

    // 分割线
    private var line: View

    init {
        isClickable = true
        isFocusable = true
        orientation = VERTICAL
        setBackgroundColor(ContextCompat.getColor(context, R.color.background_secondary))

        // 内嵌布局
        val nestedFrameLayout = FrameLayout(context)
        val layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1F)
        addView(nestedFrameLayout, layoutParams)

        // 文案
        itemName = TextView(context)
        itemName.setTextColor(ContextCompat.getColor(context, R.color.text_primary))
        itemName.textSize = context.spFromDimen(R.dimen.font_16)
        val textLP = FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        textLP.gravity = Gravity.START or Gravity.CENTER_VERTICAL
        textLP.leftMargin = context.dpFromDimen(R.dimen.spacing_16)
        nestedFrameLayout.addView(itemName, textLP)

        // 音量滑动条
        volumeSlider = VolumeSlider(context)
        val sliderLP = FrameLayout.LayoutParams(
            context.dpFromDimen(R.dimen.size_250),
            LayoutParams.WRAP_CONTENT
        )
        sliderLP.gravity = Gravity.END or Gravity.CENTER_VERTICAL
        sliderLP.marginEnd = context.dpFromDimen(R.dimen.spacing_16)
        nestedFrameLayout.addView(volumeSlider, sliderLP)

        // 分割线
        line = View(context)
        line.setBackgroundColor(ContextCompat.getColor(context, R.color.solid_line))
        val lineLP = LayoutParams(LayoutParams.MATCH_PARENT, context.dpFromDimen(R.dimen.size_1))
        lineLP.leftMargin = context.dpFromDimen(R.dimen.spacing_16)
        addView(line, lineLP)

        if (attrs != null) {
            val typedArray = context.obtainStyledAttributes(attrs, R.styleable.VolumeSetItem)
            val title = typedArray.getString(R.styleable.VolumeSetItem_volume_title)
            val progress = typedArray.getFloat(R.styleable.VolumeSetItem_volume_initial_progress, 50f)
            
            itemName.text = title ?: "音量调节"
            volumeSlider.setProgress(progress)
            
            typedArray.recycle()
        } else {
            // 默认设置
            itemName.text = "音量调节"
            volumeSlider.setProgress(50f)
        }
    }

    /**
     * 设置标题
     */
    fun setTitle(title: String) {
        itemName.text = title
    }
    
    /**
     * 设置音量
     */
    fun setVolume(volume: Float) {
        volumeSlider.setProgress(volume)
    }
    
    /**
     * 获取当前音量
     */
    fun getVolume(): Float {
        return volumeSlider.getProgress()
    }
    
    /**
     * 设置音量变化监听器
     */
    fun setOnVolumeChangeListener(listener: VolumeSlider.OnVolumeChangeListener) {
        volumeSlider.setOnVolumeChangeListener(listener)
    }
}